package com.mathdroid.gui.main.abilities.optimizer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;

import com.mathdroid.core.DroidStringWorker;
import com.mathdroid.gui.ImageLoader;
import com.mathdroid.gui.componentlist.AComponentListElement;
import com.mathdroid.gui.componentlist.Cell;
import com.mathdroid.model.Model;
import com.mathdroid.model.abilities.Ability;
import com.mathdroid.model.abilities.AbilityTemplate;

/**
 * This is a cell for the result list. It displays an ability optimizer result.
 */
public class ResultCell extends Cell {

	/**
	 * Generated UID.
	 */
	private static final long serialVersionUID = -648879539913031916L;
	
	/**
	 * The optimizer result.
	 */
	private ResultElement result = null;
	
	/**
	 * This label shows the rating of the result.
	 */
	private JLabel rating = new JLabel();
	
	/**
	 * The yellow-green bar color.
	 */
	private static final Color DROID_GREEN = new Color(172, 230, 52);
	
	/**
	 * The size of the rating text.
	 */
	private static final float HUGE_FONT = 25F;
	
	/**
	 * The constraints for this UI components.
	 */
	private final GridBagConstraints c = new GridBagConstraints();
	
	/**
	 * The space between UI components.
	 */
	private static final int SPACING = 5;
	
	/**
	 * Iterator for column indices.
	 */
	private int columns = 0;
	
	/**
	 * Button to use the result priority list.
	 */
	private final JButton apply = new JButton("Apply");
	
	/**
	 * ImageLoader.
	 */
	private final ImageLoader il = new ImageLoader();
	
	/**
	 * StringWorker.
	 */
	private final DroidStringWorker sw = new DroidStringWorker();

	/**
	 * Create a new cell.
	 * @param e The list element which has to be an instance of ResultElement.
	 */
	public ResultCell(final AComponentListElement e) {
		this.setLayout(new GridBagLayout());
		
		this.result = (ResultElement) e;
		
		this.buildApplyButton();
		this.buildRatingLabel();
		this.buildIcons();
		this.buildGlue();
	}

	/**
	 * Build the "apply" button.
	 */
	private void buildApplyButton() {
		this.c.gridx = this.columns;
		this.c.gridy = 0;
		this.c.weightx = 0;
		this.c.weighty = 0;
		this.c.insets = new Insets(SPACING, SPACING, SPACING, SPACING);
		
		this.apply.setIcon(this.il.getImage("accept"));
		
		this.apply.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(final ActionEvent e) {
				AbilityTemplate t = new AbilityTemplate();
				for (Ability a : result.getResult().getPrio()) {
					t.add(a);
				}
				Model.setAbilities(t);
			}
		});
		
		this.add(this.apply, this.c);
		this.columns++;
	}

	/**
	 * Build the icon labels.
	 */
	private void buildIcons() {
		this.c.gridy = 0;
		this.c.weightx = 0;
		this.c.weighty = 0;
		this.c.insets = new Insets(SPACING, 0, SPACING, SPACING);
		
		for (Ability a : this.result.getResult().getPrio()) {
			
			/* Only display abilities that were optimized. */
			if (a.isAvailabe() && a.isEnabled() && a.isUsedForOptimization()) {
				this.c.gridx = this.columns;
				final JLabel lbl = new JLabel(
						this.il.getAbilityIcon(a.getIconName()));
				lbl.setToolTipText("<html><b>" + a.getName() + "</b>"
						+ "</html>");
				this.add(lbl, this.c);
				this.columns++;
			}
		}
	}

	/**
	 * Build the invisible glue.
	 */
	private void buildGlue() {
		this.c.gridx = this.columns;
		this.c.gridy = 0;
		this.c.weightx = 1;
		this.c.weighty = 0;
		this.c.insets = new Insets(0, 0, 0, 0);
		
		this.add(Box.createGlue(), this.c);
		this.columns++;
	}

	/**
	 * Build the rating label.
	 */
	private void buildRatingLabel() {
		this.c.gridx = this.columns;
		this.c.gridy = 0;
		this.c.weightx = 0;
		this.c.weighty = 0;
		this.c.insets = new Insets(SPACING, 0, SPACING, SPACING);
		
		NumberFormat nf = NumberFormat.getNumberInstance();
		nf.setMaximumFractionDigits(2);
		nf.setMinimumFractionDigits(2);
		this.rating.setFont(this.rating.getFont().deriveFont(HUGE_FONT));
		this.rating.setText(
				" " + nf.format(this.result.getResult().getRating()));
		this.add(this.rating, this.c);
		this.columns++;
	}
	
	@Override
	public final void paintComponent(final Graphics g) {
		super.paintComponent(g);
		
		g.setColor(DROID_GREEN);
		g.fillRect(0, 0, 
				(int) (this.getWidth() * this.result.getResult().getRating() 
						/ this.result.getMaxRating()), 
				this.getHeight());
		
		
	}

}
